package com.gzx.plugin.data.wckj.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.gzx.plugin.data.core.consts.CompanyConstants;
import com.gzx.plugin.data.core.utils.ServerIpUtil;
import com.gzx.plugin.data.wckj.config.WckjConfig;
import com.gzx.plugin.data.wckj.mapper.DataWckjYwyhidMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.function.Supplier;

@Slf4j
@Service
@DS("data")
@RequiredArgsConstructor
public class CaseInfoScheduleService {
    @Value("${serverIp.enableIp}")
    private String serverIp;

    private final WckjConfig wckjConfig;

    private final DataWckjYwyhidMapper ywyhidDao;

    private final CaseInfoThreadPoolService caseInfoThreadPoolService;

    public void scheduleForCaseM4() {
        processCase(
                CompanyConstants.WCKJ_PROJECT_CODE_M4,
                "M4+",
                ywyhidDao::getDataListM4Top5,
                wckjConfig.getOrgCode(),
                wckjConfig.getOrgPwd()
        );
    }

    public void scheduleForCaseM7() {
        processCase(
                CompanyConstants.WCKJ_PROJECT_CODE_M7,
                "M7+",
                ywyhidDao::getDataListM7Top5,
                wckjConfig.getOrgCode(),
                wckjConfig.getOrgPwd()
        );
    }

    public void scheduleForCaseM12() {
        processCase(
                CompanyConstants.WCKJ_PROJECT_CODE_M13,
                "M12+",
                ywyhidDao::getDataListM12Top5,
                wckjConfig.getOrgCode12(),
                wckjConfig.getOrgPwd12()
        );
    }

    private void processCase(String projectCode,
                             String caseType,
                             Supplier<List<String>> dataListSupplier,
                             String orgCodeParam,
                             String orgPwdParam) {
        if (!serverIp.equals(ServerIpUtil.getServerIp())) {
            log.warn("非指定服务器IP[当前IP:{}]，跳过执行", ServerIpUtil.getServerIp());
            return;
        } else {
            log.info("当前服务器IP：{}为指定运行机器, 开始启动{}微财5.2接口......", serverIp, caseType);
        }

        try {
            List<String> ywyhidList = dataListSupplier.get();
            if (CollectionUtil.isEmpty(ywyhidList)) {
                log.info("本月{}的案子未开始推送或已处理完毕", caseType);
                return;
            }
            validateCredentials(orgCodeParam, orgPwdParam);
            caseInfoThreadPoolService.runCaseService(ywyhidList, orgCodeParam, orgPwdParam, projectCode);
        } catch (DataAccessException e) {
            log.error("{}案件数据库查询失败: {}", caseType, e.getMessage());
        } finally {
            log.info("本次{}接口查询结束", caseType);
        }
    }

    private void validateCredentials(String orgCode, String orgPwd) {
        if (StringUtils.isAnyBlank(orgCode, orgPwd)) {
            throw new IllegalArgumentException("机构认证参数不能为空");
        }
    }
}
